package com.maxcion.scalelayoutmanager.form

import com.maxcion.scalelayoutmanager.adapter.BaseViewHolder
import com.maxcion.scalelayoutmanager.adapter.PageLoadRecyclerVewAdapter

interface IAdapterConvert {

    fun getItemViewType(position: Int): Int

    fun getItemCount(): Int

    fun convert(holder: BaseViewHolder, position: Int)
}

class OnlyHorizontalAdapterConvert<T>(private val config: FormLayoutManager.Builder<T>) :
    IAdapterConvert {
    override fun getItemViewType(position: Int): Int {
        return if (position / (config.maxColumnCount) == 0) {
            TYPE_HORIZONTAL_TITLE
        } else {
            TYPE_CONTENT
        }
    }

    override fun getItemCount(): Int {
        return config.maxColumnCount * (config.contentLines.size + 1)
    }

    override fun convert(holder: BaseViewHolder, position: Int) {
        if (holder.itemViewType == TYPE_HORIZONTAL_TITLE) {
            config.contentConvert?.convertHorizontalTitle(config.horizontalTitles[position], holder)
        } else {
            config.contentConvert?.convertContent(
                config.contentLines[position / config.maxColumnCount - 1],
                holder,
                position % config.maxColumnCount
            )
        }
    }

}

class OnlyVerticalAdapterConvert<T>  constructor(val config: FormLayoutManager.Builder<T>) :
    IAdapterConvert {
    override fun getItemViewType(position: Int): Int {
        return if (position % config.maxColumnCount == 0) TYPE_VERTICAL_TITLE else TYPE_CONTENT
    }

    override fun getItemCount(): Int {

        return config.contentLines.size * (config.maxColumnCount + 1)
    }

    override fun convert(holder: BaseViewHolder, position: Int) {
        if (holder.itemViewType == TYPE_VERTICAL_TITLE) {
            config.contentConvert?.convertVerticalTitle(
                config.verticalTitles[position / (config.maxColumnCount - 1)],
                holder
            )
        } else {
            config.contentConvert?.convertContent(
                config.contentLines[position / config.maxColumnCount],
                holder,
                position % (config.maxColumnCount + 1) - 1
            )
        }
    }

}

class FullTitleAdapterConvert<T>  constructor(val config: FormLayoutManager.Builder<T>) :
    IAdapterConvert {
    override fun getItemViewType(position: Int): Int {
        return when {
            position == 0 -> {
                TYPE_TITLE_EMPTY
            }
            position / (config.maxColumnCount + 1) == 0 -> {
                TYPE_HORIZONTAL_TITLE
            }
            position % (config.maxColumnCount + 1) == 0 -> {
                TYPE_VERTICAL_TITLE
            }
            else -> {
                TYPE_CONTENT
            }
        }
    }

    override fun getItemCount(): Int {
        return 1 + (config.contentLines.size + 1) * (config.maxColumnCount + 1)

    }

    override fun convert(holder: BaseViewHolder, position: Int) {
        when (holder.itemViewType) {
            TYPE_VERTICAL_TITLE -> {
                config.contentConvert?.convertVerticalTitle(
                    config.verticalTitles[position / (config.maxColumnCount + 1) - 1],
                    holder
                )
            }
            TYPE_HORIZONTAL_TITLE -> {
                config.contentConvert?.convertHorizontalTitle(
                    config.horizontalTitles[position - 1],
                    holder
                )
            }

            TYPE_CONTENT -> {
                config.contentConvert?.convertContent(
                    config.contentLines[position / (config.maxColumnCount + 1) - 1],
                    holder,
                    position % config.maxColumnCount - 1
                )
            }
        }
    }

}